package com.danco.sn.model.dao;

import java.util.Date;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

import com.danco.sn.apientities.IUser;

public class UserDAO extends GenericDAO<IUser> {

	private static final String SEX = "sex";
	private static final String BIRTHDAY = "birthday";
	private static final String HOME_CITY = "homeCity";
	private static final String LOGIN = "login";
	// private static final String DELIMITER = " ";
	private static final String LAST_NAME = "lastName";
	private static final String FIRST_NAME = "firstName";

	public UserDAO() {
		super(IUser.class);
	}

	/*
	 * public IUser autorization(String login, Session session) throws
	 * HibernateException { IUser user = (IUser) super.read(LOGIN, login,
	 * session); return user; }
	 */

	@SuppressWarnings("unchecked")
	public List<IUser> searchPeople(Session session, String name,
			String lastName, String login, String city, Date birthday,
			String sex) throws HibernateException {
		Criteria criteria = session.createCriteria(IUser.class);
		if (name != null) {
			criteria.add(Restrictions.like(FIRST_NAME, name.trim()));
		}
		if (lastName != null) {
			criteria.add(Restrictions.like(LAST_NAME, lastName.trim()));
		}
		if (login != null) {
			criteria.add(Restrictions.like(LOGIN, login));
		}
		if (city != null) {
			criteria.add(Restrictions.like(HOME_CITY, city));
		}
		if (birthday != null) {
			criteria.add(Restrictions.eq(BIRTHDAY, birthday));
		}
		if (sex != null) {
			criteria.add(Restrictions.eq(SEX, sex));
		}
		List<IUser> listUser = criteria.list();

		if (name == null && lastName == null && login == null && city == null
				&& birthday == null && sex == null) {
			return null;
		}

		return listUser;
	}
}
